/*****************************************************************************/
/** \file comm.h
**
** 
**
** History:
** 
*****************************************************************************/
#ifndef  __COMM_H__
#define  __COMM_H__

/*****************************************************************************/
/* Include files */
/*****************************************************************************/

/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/*****************************************************************************/
#define	UART_THREIE_EN_Pos			(1)
#define UART_THREIE_EN_Msk			(0x01<<UART_THREIE_EN_Pos)
#define	UART_RBRIE_EN_Pos			(0)
#define UART_RBRIE_EN_Msk			(0x01<<UART_RBRIE_EN_Pos)

#define	UART_INTID_RX_Pos			(2)
#define UART_INTID_RX_Msk			(0x01<<UART_INTID_RX_Pos)
#define	UART_INTID_TX_Pos			(1)
#define UART_INTID_TX_Msk			(0x01<<UART_INTID_TX_Pos)

#define UART1_READRX_FLAG			(UART1->IIR & 0x04)
#define UART1_READTX_FLAG			(UART1->IIR & 0x02)

#define UART1_RXINT_ENABLE			{UART1->IER |= (0x01UL<<0);}
#define UART1_RXINT_DISABLE			{UART1->IER &= ~(0x01UL<<0);}
#define UART1_TXINT_ENABLE     		{UART1->IER |= (0x01UL<<1);}
#define UART1_TXINT_DISABLE     	{UART1->IER &= ~(0x01UL<<1);}

#define UART_EnableIRQ(Msk)         {UART1->IER |= (Msk);}
#define UART_DisableIRQ(Msk)        {UART1->IER &= ~(Msk);}

#define	PARA_COMD_LENTH				34

/*****************************************************************************/
/* Global type definitions ('typedef') */
/*****************************************************************************/
typedef struct S_UART
{
	u8 u8ucSend[100];
	u8 u8ucRecv[100];
	u8 u8ucSendBYTELen; 
	u8 u8ucSendWORDLen; 
	u8 u8ucSendCnt;    
	u8 u8ucRecvLen;
	u8 u8ucRevFlg;
	u32 u32rxnodata_time;
	
	u8	motor_para_updtF:1;
	u8	motor_start_para_updtF:1;
	u8	motor_pro8board_para_updtF:1;
	u8	motor_control_para_updtF:1;
	union
	{
		u8 b[2];
		u16 w;
	}motor_para[PARA_COMD_LENTH];
	
	union
	{
		u8 b[2];
		u16 w;
	}motor_start_para[PARA_COMD_LENTH];
	
	union
	{
		u8 b[2];
		u16 w;
	}motor_pro8board_para[PARA_COMD_LENTH];
	
	union
	{
		u8 b[2];
		u16 w;
	}motor_control_para[PARA_COMD_LENTH];
	
	u8	debug_modeF:1;
}S_UART;


typedef struct 
{
	s32		Ls;
	s32		Rs;
	s32		Paris;
	s32		Ke;
	s32		Voltage;
	s32		Current;
	s32		Power;
	
}BASEPARA;

typedef struct 
{
	u8		RunFlag:1;
	u8		RLmeas_Flag;
	s32		RunSpeed;
	u8		DataSaveFlag;
	u8		Md_Control;
	u8		Md_Sensor:1;
	u8		Md_Shunt:1;
	u8		Md_Svpwm:1;
	u8		Md_Wind:1;
	u8		MotorDir:1;
	u8		Md_Debug;
	u8		Md_IPD;
	u8		Md_Comm;	
	u8		Md_DeadComp:1;	
}CTRPARA;

typedef struct 
{	
	s32		Dkp;
	s32		Dki;
	s32		Dout_max;
	s32		Dout_min;
	s32		Qkp;
	s32		Qki;
	s32		Qout_max;
	s32		Qout_min;
	s32		Ekp;
	s32		Eki;
	s32		Eout_max;
	s32		Eout_min;
	s32		Start_Current;
}WINDPARA;

typedef struct 
{
	u8		mode;
	u16		rpm_init;
	u16		rpm_final;
	s32 	align_current;
	u32		align_time;
	u32		align_addtime;
	u32		align_holdtime;
	u32		align_add_k;	
	s32		run_current_init;
	s32		run_current_final;
	u32		run_time;
	s32		Dkp;
	s32		Dki;
	s32		Dout_max;
	s32		Dout_min;
	s32		Qkp;
	s32		Qki;
	s32		Qout_max;
	s32		Qout_min;
	s32		Ekp;
	s32		Eki;
	s32		Eout_max;
	s32		Eout_min;
	s32		EInt_max;
	s32		EInt_min;
	s32		Etheta;
	s32		Offset_Ki;
	s32		Offset_Ku;
	s32		ERampInc;
	s32		ERampDec;
	u32		Hold_Time;
}STARTPARA;

typedef struct 
{
	s32		Dkp;
	s32		Dki;
	s32		Dout_max;
	s32		Dout_min;
	s32		Qkp;
	s32		Qki;
	s32		Qout_max;
	s32		Qout_min;
	s32		Wkp;
	s32		Wki;
	s32		Wout_max;
	s32		Wout_min;
	s32		Pkp;
	s32		Pki;
	s32		Pout_max;
	s32		Pout_min;
	s32		OffsetTheta;
}FOCPARA;

typedef struct 
{
	s32		Ekp;
	s32		Eki;
	s32		Eout_max;
	s32		Eout_min;
	s32		Ki;
	s32		Ku;
	s32		Kslide;
	s32		KE;
	s32		Kslf;
	s32		KWe;
	s32		KG;
	u8		Theta;	
	s32		OffsetTheta;
}OBSEPARA;

typedef struct 
{	
	s32		Samp_IP;
	s32		Samp_Ibus;
	s32		Samp_Vbus;
	s32		Samp_Vctrl;
	s32		Samp_Vtem;
	s32		Speed_Val;
	s32		Power_Val;
	s32		PhaseK;
}LPFPARA;

typedef struct 
{
	s32		Val_Vbus;
	s32		Val_Ibus;
}UPPARA;

typedef struct	
{
	BASEPARA	Base;
	CTRPARA		Ctrl;
	WINDPARA	Wind;
	STARTPARA	Start;
	FOCPARA		FOC;
	OBSEPARA	Obse;
	LPFPARA		Lpf;
	UPPARA		Upload;
}MCPARA;

/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source) */
/*****************************************************************************/
extern	MCPARA		Para;
extern	S_UART 		UART_MT;
/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source) */
/*****************************************************************************/
extern	void 		Task_UI(void);
extern  void 		Uart_Deal(void);


#endif	/* __COMM_H__ */

